package com.link.base.core.RSAUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @auther HuangLJ
 * @throws Exception
 * @Date 2018-12-26 21:20
 * @Comments Dmp本地服务器加密用
 */
public class DmpDecode {

    //测试公钥
    private static String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrXry06MCcJtl2naLK3+H3tqyB\n"
            + "bYk9DfCtbs9/jC1bMkZwGPP2kTVZpgrDwyDn8qjmstm0p5eWdEClOJ0xauKZUPQw\n"
            + "RNoXxmGOrdUG1QxmLAvKzFzmcpSfDXeH3MJuPAC6p8LA+UxGvIIZHT3MIxF4NZ3t\n"
            + "r8pLpGVqzHUg66f/xQIDAQAB";

    private static String privateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKtevLTowJwm2Xad\n"
            + "osrf4fe2rIFtiT0N8K1uz3+MLVsyRnAY8/aRNVmmCsPDIOfyqOay2bSnl5Z0QKU4\n"
            + "nTFq4plQ9DBE2hfGYY6t1QbVDGYsC8rMXOZylJ8Nd4fcwm48ALqnwsD5TEa8ghkd\n"
            + "PcwjEXg1ne2vykukZWrMdSDrp//FAgMBAAECgYBZVtxeG/E99urr8aZKyYjOUUeB\n"
            + "eg9P6xKL9/bo8bU7k+wwyHF3G6CKiguWnwVqppDzJE5m5fAbGaQR/n/EoYpbtPh3\n"
            + "870tvCtkGg2iWP7dU5eOwhrpCAQvM4qm6QLEJzob9wGbv+Psf8m7Klu3oDlBdrtT\n"
            + "Dy1sOC1IcWOisBs2QQJBANNc3cm58kagQyjqJSrOY0OG3S/quooBIqoBbJ9sKV/E\n"
            + "hSLVRraBBrR0v2uzKnheWcNuEKavpfFQIkq3Uelf9L0CQQDPj7PbMK36cv4usSdb\n"
            + "zFe9GWNyJNocHh/Do/GzrmA9OGx6cN29Tdy5QX5Jry6AoNqPpJLDTBq9k6rpnxZj\n"
            + "NZupAkB/5pBng9gyGMTLO3UmRCIZOMEcP+SFok5Bpf7/McqPozIVdA4IXmeaR5OW\n"
            + "LqMil6Wl9zKoS14VerJlmC0/FcvZAkBI3KtIgLgyOPfdsDae1QbfzO/J/AHM6Q5B\n"
            + "gWWxq78A/rOl78UHVTPsnH8WorZyVQUkc6NJh4fDkPjodf5H6WOxAkADmj53ESy2\n"
            + "TE++w74A9+1hTt0BynmLzTgERKassGOvuYk2qrc9LjK84D0ENmKgqY8ZBhdBp86k\n"
            + "LCoJbUIzmKEr";

    public static String dmpDecode(Map<String, String> map) throws Exception {
        String aesKey = RSAUtils.decryptByPrivateKeyAndBase64(map.get("aesKey"), privateKey);
        return AESUtils.decryptBase64(map.get("msg"), aesKey);
    }

    public static Map<String, String> dmpEncode(String data) throws Exception {
        Map map = new HashMap(16);
        //获取uuid ，用来做aes的秘钥
        String primaryKey = UUID.randomUUID().toString();
        //通过aes加密
        map.put("msg", AESUtils.encryptBase64(primaryKey, data));
        //通过rsa加密aes秘钥
        String aeskey = RSAUtils.getBase64ByPublicKey(primaryKey, publicKey);
        map.put("aesKey", aeskey);
        return map;
    }
}
